//*****************************************************************************
//
//! \file analogButtons.c
//! \brief Driver for analogButtons.
//! \version 1.0.0.0
//! \date 03/11/2013
//! \author Nemon
//! \copy
//!
//! Copyright (c)  2011, Nemon
//! All rights reserved.
//!
//! Redistribution and use in source and binary forms, with or without
//! modification, are permitted provided that the following conditions
//! are met:
//!
//!     * Redistributions of source code must retain the above copyright
//! notice, this list of conditions and the following disclaimer.
//!     * Redistributions in binary form must reproduce the above copyright
//! notice, this list of conditions and the following disclaimer in the
//! documentation and/or other materials provided with the distribution.
//!     * Neither the name of the <ORGANIZATION> nor the names of its
//! contributors may be used to endorse or promote products derived
//! from this software without specific prior written permission.
//!
//! THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
//! AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
//! IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
//! ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
//! LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
//! CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
//! SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
//! INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
//! CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
//! ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
//! THE POSSIBILITY OF SUCH DAMAGE.
//
//*****************************************************************************

#include "xhw_types.h"
#include "xhw_memmap.h"
#include "xhw_ints.h"
#include "xdebug.h"
#include "xsysctl.h"
#include "xgpio.h"
#include "xadc.h"
#include "cookie.h"
#include "analogButtons.h"

#define BTN_MASK 0xFFF

//*****************************************************************************
//
//! \brief make analog Buttons init.
//!
//! make analog Buttons init.
//!
//! \return None
//
//*****************************************************************************
void analogButtonsInit(void)
{
	//init adc buttons
	xSysCtlPeripheralEnable(analogButtons_PERIPH);
	xSPinTypeADC(analogButtons_ADC, analogButtons_PIN);
	xADCConfigure(analogButtons_BASE, xADC_MODE_SCAN_CONTINUOUS, ADC_TRIGGER_PROCESSOR);
	xADCStepConfigure(analogButtons_BASE, 0, xADC_CTL_CH0);
	xADCEnable(analogButtons_BASE);
	xADCProcessorTrigger(analogButtons_BASE);
}


//*****************************************************************************
//
//! \brief get current pressed Button index.
//!
//! get current pressed Button index.
//!
//! \return int the index value(-1 when not pressed)
//
//*****************************************************************************
signed int analogButtonsGetPressedIndex(unsigned int resistances_amount,unsigned int * resistances)
{
	signed int i=-1;
    unsigned long v,ulValueLength, ulData[8];
    unsigned long v_last,v_curr;
	unsigned int r_a,r_b;
	//unsigned long i_resistances[]=analogButtons_Resistances_Array;
	//unsigned int resistances_amount=analogButtons_Resistances_Number;
	r_a=0;
	r_b=resistances[0];


	//adc buttons
    ulValueLength = xADCDataGet(analogButtons_BASE, ulData);
    xSysCtlDelay(1000);
    v = ulData[0] & BTN_MASK;
    if(v==0)
    {
    	return 0;
    }
    else if(resistances_amount>1)
    {
    	v_last=0;
    	r_a=0;
    	r_b=resistances[0];
    	for(i=0;i<resistances_amount-1;i++)
		{
    		r_a+=resistances[i+1];
    		r_b+=resistances[i+1];
    		v_curr= BTN_MASK * r_a/r_b;
			//printf("[%d,%d]%d\n",v_last,v_curr,(v_last+v_curr)/2);
			if(v<(v_last+v_curr)/2)
			{
				return i;
				break;
			}
			v_last=v_curr;
		}
    	if(v<(v_last+ BTN_MASK )/2)
    	{
    		//printf("{%d,%d}%d\n",v_last,0xFFF,(v_last+0xFFF)/2);
			return resistances_amount-1;
    	}
    	else
    	{
    		return -1;
    	}
    }else
    {
    	return -1;
    }
}

